package com.example.recordingpen_datacollection.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.recordingpen_datacollection.Utils.DateUtils.TimeJudgeUtils;
import com.example.recordingpen_datacollection.Utils.HtmlLayoutUtil.BDHtmlLayoutV2Util;
import com.example.recordingpen_datacollection.Utils.String.StringUtils;
import com.example.recordingpen_datacollection.entity.Businessdata;
import com.example.recordingpen_datacollection.entity.DosageStatistics;
import com.example.recordingpen_datacollection.entity.Vo.AISumUp;
import com.example.recordingpen_datacollection.entity.Vo.AIZFYVo;
import com.example.recordingpen_datacollection.redisService.RedisService;
import com.example.recordingpen_datacollection.result.RLIST;
import com.example.recordingpen_datacollection.result.exceptionhandler.JWException;
import com.example.recordingpen_datacollection.service.AIService;
import com.example.recordingpen_datacollection.service.BusinessdataService;
import com.example.recordingpen_datacollection.service.DosageStatisticsService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/AI")
public class AIController {
    @Autowired
    private AIService aiService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private BusinessdataService businessdataService;
    @Autowired
    private DosageStatisticsService dosageStatisticsService;
    @ApiOperation(value = "AI会议  字数使用策略")
    @PostMapping("/txtSumUp")
    public RLIST txtSumUp(@RequestBody AISumUp aiSumUp, HttpServletRequest request){

        //6ab0878d-4cc8-411e-a6e0-b3fb7013836b
        String token = request.getHeader("token");
        if( token.length() != 36 ){
            return RLIST.error().message("无权操作");
        }
        Integer value = (Integer) redisService.getValue(token);
        String txt = aiSumUp.getContent();

        if(StringUtils.isEmpty(aiSumUp.getTextName())){
            aiSumUp.setTextIs(false);
        }
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("login_id",value);
        Businessdata businessdataOne = businessdataService.getOne(wrapper);
        if(businessdataOne == null){
            return RLIST.error().message("未查询到数据");
        }
        if(businessdataOne.getDosage()< txt.length()){
            return RLIST.error().message("文本字数超出用量");
        }

        if(aiSumUp.getServerType() ==3 && aiSumUp.getContent().length() >= 8192){
            return RLIST.error().message("讯飞不支持大文本，不允许超过8000字");
        }

        if(aiSumUp.isTextIs()){
            QueryWrapper<DosageStatistics> isTextWrapper = new QueryWrapper();
            isTextWrapper.eq("login_id",value);
            isTextWrapper.eq("text_name",aiSumUp.getTextName());
            isTextWrapper.orderByDesc("id");
            List<DosageStatistics> list = dosageStatisticsService.list(isTextWrapper);
//            System.out.println(list.get(0).getFinalResult()  +"   "+value+"   "+aiSumUp.getTextName() + "  "+list.size());
            if(list != null && list.size() >0){
                return RLIST.ok().data("data",list.get(0).getFinalResult());
            }

        }
        String txtSumUp = aiService.txtSumUp(aiSumUp,businessdataOne,true);
        BDHtmlLayoutV2Util bdHtmlLayoutV2Util = new BDHtmlLayoutV2Util();
        String txtSumUpText =null;
        if(!txtSumUp.contains("\n")){
            txtSumUpText = bdHtmlLayoutV2Util.convertToTXT(txtSumUp);
        } else {
            txtSumUpText =txtSumUp;
        }

        if((aiSumUp.getServerType() == 5 || aiSumUp.getServerType() ==4) && aiSumUp.isFormat()){

            return RLIST.ok().data("data",bdHtmlLayoutV2Util.convertToHtml(txtSumUpText ));

        }
        return RLIST.ok().data("data",txtSumUpText);

//        return RLIST.ok().dta("data",txtSumUp);
    }

    @ApiOperation(value = " AI会议  VIP时间策略")
    @PostMapping("/VIPtxtSumUp")
    private RLIST VIPtxtSumUp(@RequestBody AISumUp aiSumUp, HttpServletRequest request){
//        System.out.println("请求时间开始-------------》"+ TimeJudgeUtils.getNowDate());
        String token = request.getHeader("token");
        Integer value = (Integer) redisService.getValue(token);
        if( token.length() != 36 ){
            return RLIST.error().message("无权操作");
        }
        String txt = aiSumUp.getContent();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("login_id",value);
        Businessdata businessdataOne = businessdataService.getOne(wrapper);
        if(businessdataOne == null){
            return RLIST.error().message("未查询到数据");
        }
        //开始时间  结束时间
        String getEndTime = TimeJudgeUtils.strData(businessdataOne.getEndTime());
        boolean b = TimeJudgeUtils.judgeTime(TimeJudgeUtils.getNowDate(), getEndTime);
        if(b){
            return RLIST.error().message("VIP已过期");
        }


        if(aiSumUp.isTextIs()){
            QueryWrapper<DosageStatistics> isTextWrapper = new QueryWrapper();
            isTextWrapper.eq("login_id",value);
            isTextWrapper.eq("text_name",aiSumUp.getTextName());
            isTextWrapper.orderByDesc("id");
            List<DosageStatistics> list = dosageStatisticsService.list(isTextWrapper);
//            System.out.println(list.get(0).getFinalResult()  +"   "+value+"   "+aiSumUp.getTextName() + "  "+list.size());
            if(list != null && list.size() >0){

                BDHtmlLayoutV2Util bdHtmlLayoutV2Util = new BDHtmlLayoutV2Util();
                String txtSumUpText =null;
                if( !list.get(0).getFinalResult().contains("\n") ){
                    txtSumUpText = bdHtmlLayoutV2Util.convertToTXT(list.get(0).getFinalResult());
                } else {
                    txtSumUpText =list.get(0).getFinalResult();
                }
//                String txtSumUpText = bdHtmlLayoutV2Util.convertToTXT(list.get(0).getFinalResult());
                if((aiSumUp.getServerType() == 5 || aiSumUp.getServerType() ==4) && aiSumUp.isFormat()){

                    return RLIST.ok().data("data",bdHtmlLayoutV2Util.convertToHtml(txtSumUpText));

                }
                return RLIST.ok().data("data",txtSumUpText);
            }

        }

//        System.out.println("执行txtSumUp-------------》"+ TimeJudgeUtils.getNowDate());
        String txtSumUp = aiService.txtSumUp(aiSumUp,businessdataOne,false);
        BDHtmlLayoutV2Util bdHtmlLayoutV2Util = new BDHtmlLayoutV2Util();
        String txtSumUpText =null;
        if(!txtSumUp.contains("\n")){
            txtSumUpText = bdHtmlLayoutV2Util.convertToTXT(txtSumUp);
        } else {
            txtSumUpText =txtSumUp;
        }
        if((aiSumUp.getServerType() == 5 || aiSumUp.getServerType() ==4) && aiSumUp.isFormat()){

            return RLIST.ok().data("data",bdHtmlLayoutV2Util.convertToHtml(txtSumUpText ));

        }
        return RLIST.ok().data("data",txtSumUpText);
    }


    @ApiOperation(value = " AI问答接口 无策略")
    @PostMapping("/AItxt")
    private RLIST AItxt(@RequestBody AIZFYVo aiSumUp, HttpServletRequest request){

        String txtSumUp = aiService.AITxtUp(aiSumUp);

        return RLIST.ok().data("data",txtSumUp);
    }


    @ApiOperation(value = "异步队列接口")
    @PostMapping("/AIasynchronousTask")
    private RLIST AIasynchronousTask(@RequestBody AISumUp aiSumUp, HttpServletRequest request){
//        System.out.println("请求时间开始-------------》"+ TimeJudgeUtils.getNowDate());
        String token = request.getHeader("token");
        Integer value = (Integer) redisService.getValue(token);
        if( token.length() != 36 ){
            return RLIST.error().message("无权操作");
        }
        String txt = aiSumUp.getContent();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("login_id",value);
        Businessdata businessdataOne = businessdataService.getOne(wrapper);
        if(businessdataOne == null){
            return RLIST.error().message("未查询到数据");
        }
        //开始时间  结束时间
        String getEndTime = TimeJudgeUtils.strData(businessdataOne.getEndTime());
        boolean b = TimeJudgeUtils.judgeTime(TimeJudgeUtils.getNowDate(), getEndTime);
        if(b){
            return RLIST.error().message("VIP已过期");
        }

        RLIST rlist = dosageStatisticsService.setDSMessage(aiSumUp,value);



        return rlist;
    }


}
